package com.zy.core.data.jpa.support.entity.page;

import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页对象
 * 2018-3-11
 * @param <T>
 */

public class PageBean<T extends Object> implements Serializable {

	//页码参数
	public final static String PARAMS_PAGE="page";
	//页大小参数
	public final static String PARAMS_PAGESIZE="pageSize";
	//条件参数对象
	public final static String PARAMS_BEAN="bean";
	
	public final static int DefaultPageNumber = 0;
	public final static int DefaultDataTotal = 0;
	public final static int DefaultPageSize = 10;// 50
	private int pageSize = DefaultPageSize;
	private int pageNumber = DefaultPageNumber;
	private List<T> rows = null;
	//新增底部汇总行
	private List<T> footerRows=null;
	private long total = DefaultDataTotal;

	private int pageNumberSize;// 页面显示数字的个数
	private List<PageBean.Number> numberList;// 存储页数数字list;
	public PageBean(){
		
	}
	public PageBean(int pageNo,int pageSize){
		this.pageSize = 0 > pageSize ? DefaultPageSize : pageSize;
		this.pageNumber = Math.max(DefaultPageNumber, pageNo);
	}
	public PageBean(int pageNo, long total, int pageSize, List<T> data){
		this.pageSize = 0 > pageSize ? DefaultPageSize : pageSize;
		this.pageNumber = Math.max(DefaultPageNumber, pageNo);
		this.total = Math.max(DefaultDataTotal, total);
		this.rows = data;
	}
	public long getTotal() {
		return this.total;
	}

	public int getPagesCount() {
		if (this.pageSize > 0) {
			return (int) (this.total / this.pageSize + (this.total % this.pageSize == 0 ? 0 : 1));
		} else {
			return 1;
		}
	}

	/**
	 * The first page is 0
	 * 
	 * @return
	 */
	public int getPageSize() {
		return this.pageSize;
	}
	
	/**
	 * @param rows the rows to set
	 */
	public void setRows(List<T> rows) {
		this.rows = rows;
	}
	
	public List<T> getRows() {
		return this.rows;
	}

	public List<T> getFooterRows() {
		return footerRows;
	}
	public void setFooterRows(List<T> footerRows) {
		this.footerRows = footerRows;
	}
	public int getPageNumber() {
		return this.pageNumber;
	}

	public boolean hasNextPage() {
		return this.getPageNumber() < this.getPagesCount() - 1;
	}

	public boolean hasPreviousPage() {
		return this.getPageNumber() > 0;
	}

	/**
	 * 类似于google的分页数字
	 * 
	 * @return
	 */
	public List<PageBean.Number> getNumberList() {
		numberList = new ArrayList<>();
		int start = 0;
		int end = 0;

		if (pageNumber >= 0 && pageNumber < pageNumberSize) {
			start = 0;
			end = this.pageNumberSize;
		} else {
			start = pageNumber - (pageNumberSize - 2);
			end = pageNumber + (pageNumberSize - 1);
		}

		if (end > getPagesCount()) {
			end = getPagesCount();
		}
		if (pageNumber >= 0) {
			for (int i = start; i < end; i++) {
				Number number = new Number();
				if (pageNumber == i)
					number.setCurrentNumber(true);
				else
					number.setCurrentNumber(false);
				number.setNumber(i);
				numberList.add(number);
			}
		}
		return numberList;
	}

	public void setNumberList(List<PageBean.Number> numberList) {
		this.numberList = numberList;
	}

	public int getPageNumberSize() {
		return pageNumberSize;
	}

	public void setPageNumberSize(int pageNumberSize) {
		this.pageNumberSize = pageNumberSize;
	}
	
	/**
	 * 创建内存分页数据
	 * @param rows
	 * @return
	 */
	public  PageBean<T> createMemPageBean(List<T> rows){
		List<T> data=new ArrayList<T>();
		if (rows.size() != 0) {
			for (int i = 0; i < (rows.size() <= pageSize ? rows
					.size() : pageSize); i++) {
				if ((pageNumber + i) >= rows.size()) {
					break;
				}
				data.add(rows.get(pageNumber + i));
			}
		}
		this.rows=data;
		this.total=rows.size();
		return this;
	}

	@Override
	public String toString() {
		StringBuilder pageBuilder = new StringBuilder("\nQuery.Page Info");
		pageBuilder.append("\nPage.total : " + this.total);
		pageBuilder.append("\nPage.pageNumber : " + this.pageNumber);
		pageBuilder.append("\nPage.pageSize : " + this.pageSize);
		pageBuilder.append("\nPage.pageDataCount : " + this.rows.size());
		if (!CollectionUtils.isEmpty(rows)) {
			for (T entry : rows) {
				pageBuilder.append("\n" + entry.toString());
			}
		}
		return pageBuilder.toString();
	}
	/**
	 * 初始化分页数据
	 * @param data 分页行数据
	 * @param total 分页总数
	 * 2014-10-18
	 * @return
	 */
	public PageBean<T> init(List<T> data,long total){
		this.rows=data;
		this.total=total;
		return this;
	}
	/**
	 * 获得分页数据SQL
	 * @param rowsql
	 * @param orderBy
	 * 2014-10-18
	 * @return
	 */
	public String toPageRowsSQL(String rowsql,String orderBy) {
		StringBuilder sql=new StringBuilder(" select R.* from ( ")
		 .append(" select *,(ROW_NUMBER() OVER(ORDER BY ID)) as NUM FROM ( ")
		 .append(rowsql).append(" ) as p ")
		 .append(" ) as R ")
		 .append(" where R.NUM between ")
		 .append(this.pageNumber).append(" and ").append(this.pageNumber+this.pageSize);
		if(orderBy!=null && !"".equals(orderBy)){
			sql.append(orderBy);
		}
		return sql.toString();
	}
	/**
	 * 获得分页数据汇总SQL
	 * @param rowsql
	 * @param primarykey
	 * 2014-10-18
	 * @return
	 */
	public String toPageTotalSQL(String rowsql,String primarykey) {
		if(primarykey==null || "".equals(primarykey)){
			primarykey="*";
		}
		StringBuilder sql=new StringBuilder(" select count(").append(primarykey).append(")")
		 .append(" from(").append(rowsql).append(" ) as T");
		return sql.toString();
	}
	
	
	
	public class Number {

		private int number;
		private boolean currentNumber;
		public int getNumber() {
			return number;
		}
		public void setNumber(int number) {
			this.number = number;
		}
		public boolean isCurrentNumber() {
			return currentNumber;
		}
		public void setCurrentNumber(boolean currentNumber) {
			this.currentNumber = currentNumber;
		}
	}
	
}